package top.chenfu.blog.dao;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
import top.chenfu.blog.entity.Pv;

import java.util.List;
import java.util.Map;

/**
 * @Auther chenfu
 * @2019/11/1 12:27
 * @Desc
 */
@Repository
public interface PvMapper extends Mapper<Pv> {

    @SelectProvider(type = PvMapperProvider.class, method = "articlePageViewStatistic")
    List<Map<String, Object>> articlePageViewStatistic();

    @SelectProvider(type = PvMapperProvider.class, method = "pvStatistic")
    List<Map<String, Object>> pvStatistic();

    List<String> selectDateByUid(Long uid);

    List<Integer> selectPvStatistics(Long uid);

    @Slf4j
    class PvMapperProvider {

        public String articlePageViewStatistic() {
            SQL sql = new SQL();
            sql.SELECT("IFNULL(sum(a.pageView),0) AS totalPv,a.uid AS uid")
                    .FROM("article AS a")
                    .GROUP_BY("a.uid")
                    .ORDER_BY("a.uid");
            log.info("articleStatistic-sql:{}", sql.toString());
            return sql.toString();
        }

        public String pvStatistic() {
            SQL sql = new SQL();
            sql.SELECT("IFNULL(sum(pv.pv),0) AS pv,pv.uid AS uid")
                    .FROM("pv")
                    .GROUP_BY("pv.uid")
                    .ORDER_BY("pv.uid");
            log.info("pvStatistic-sql:{}", sql.toString());
            return sql.toString();
        }

    }
}
